package learn.queue;

import java.util.Scanner;

public class CirCleQueueArrayDemo {
    public static void main(String[] args) {
        CirCleQueueArray queue = new CirCleQueueArray(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show)显示队列");
            System.out.println("e(exit)退出程序");
            System.out.println("a(add)添加数据到队列");
            System.out.println("g(get)取出队列数据");
            System.out.println("h(hear)显示队列头部");
            key = scanner.next().charAt(0);//接收一个字符串

            switch (key) {
                case 's':
                    try {
                        queue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    System.out.println("输入一个数字");
                    int num = scanner.nextInt();
                    try {
                        queue.addQueue(num);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        System.out.printf("取出的数字为:%d\n", queue.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.printf("头部的数据为: %d\n", queue.headQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

class CirCleQueueArray {
    int maxSize;
    int front;
    int rear;
    int[] arr;

    public CirCleQueueArray(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public void addQueue(int num) {
        if (isFull()) {
            throw new RuntimeException("队列已满");
        }
        arr[rear] = num;
        rear = (rear + 1) % maxSize;//环形数组，越界归零
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int num = arr[front];
        front = (front + 1) % maxSize;
        return num;
    }

    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }

    /**
     * @return int
     */
    private int size() {
        return (rear + maxSize - front) % maxSize;
    }
}
